<template>
  <div class="components-input-demo-presuffix">
    <a-input @click="openModal" :placeholder="placeholder || $t('m.components.label.qing-dian-ji-xuan-ze-bu-men')" v-model="departNames" readOnly :disabled="disabled || readOnly">
      <ych-icon slot="prefix" type="cluster" title="$t('m.components.label.bu-men-xuan-ze-kong-jian')"/>
      <ych-icon v-if="departIds && !disabled && !readOnly" slot="suffix" type="close-circle" @click="handleEmpty" :title="$t('m.common.button.clear')"/>
    </a-input>

    <ych-select-depart-dialog
      ref="innerDepartSelectModal"
      :currentOrgAsRoot="currentOrgAsRoot"
      :filterAuth="filterAuth"
      :multi="multi"
      :dept-id="customDeptId"
      :checkableLinkage="checkableLinkage"
      :onlyCheckDept="onlyCheckDept"
      :orgTreeShowDeptAbbr="orgTreeShowDeptAbbr"
      :customOrgOrderSql="customOrgOrderSql"
      :customOrgFilterSql="customOrgFilterSql"
      @ok="handleOK"/>
  </div>
</template>

<script>

  import YchSelectDepartDialog from './YchSelectDepartDialog'
  import { getAction } from '@/api/manage'
  import YchOrg from "@/api/YchOrg"
  import config from '@/settings'

  export default {
    name: 'YchSelectDepart',
    components: {
      YchSelectDepartDialog
    },
    props: {
      multi: Boolean,
      value: String,
      disabled: {
        type: Boolean,
        default: false
      },
      readOnly: {
        type: Boolean,
        default: false
      },
      placeholder: {
        type: String,
      },
      currentOrgAsRoot:{
        type:Boolean,
        default:false
      },
      filterAuth:{
        type:Boolean,
        default:false
      },
      checkableLinkage:{
        type:Boolean,
        default:false
      },
      onlyCheckDept:{
        type:Boolean,
        default:false
      },
      orgTreeShowDeptAbbr:{
        type:Boolean,
        default:config.orgTreeShowDeptAbbr
      },
      // 自定义返回字段，默认返回 id
      customReturnField: {
        type: String,
        default: 'id'
      },
      customOrgOrderSql: String, // 自定义组织树排序Sql
      customOrgFilterSql: String, // 自定义组织树过滤Sql
    },
    data() {
      return {
        departNames: null,
        departIds: null,
        rows: [],
        customDeptId: null,
        url: {
          queryByIds: YchOrg.queryOrgListByIds,
          queryByCodes: YchOrg.queryOrgListByCodes,
        }
      }
    },
    watch: {
      value: {
        handler(val) {
          this.departIds = val
          this.initNames()
        },
        immediate: true
      }
    },
    methods: {
      setCustomDeptId(customDeptId) {
        this.customDeptId = customDeptId
      },
      initNames() {
        if (this.departIds) {
          if(this.customReturnField === 'id'){
            getAction(this.url.queryByIds, { ids: this.departIds }).then((res) => {
              if (res.success) {
                this.rows = res.result || []

                this.departNames = this.rows.map(item => this.orgTreeShowDeptAbbr && item.orgNameAbbr ? item.orgNameAbbr : item.orgName).join(',')

                this.$emit("setName", this.departNames)
              }
            })
          } else if(this.customReturnField === 'orgCode'){
            getAction(this.url.queryByCodes, { codes: this.departIds }).then((res) => {
              if (res.success) {
                this.rows = res.result || []

                this.departNames = this.rows.map(item => this.orgTreeShowDeptAbbr && item.orgNameAbbr ? item.orgNameAbbr : item.orgName).join(',')

                this.$emit("setName", this.departNames)
              }
            })
          }
        } else {
          this.rows = []
          this.departNames = null

          this.$emit("setName", this.departNames)
        }
      },
      openModal() {
        this.$refs.innerDepartSelectModal.show(this.rows)
      },
      handleOK(rows) {
        let value = null
        if (!rows || rows.length === 0) {
          this.rows = []
          this.departNames = null
          this.departIds = null
        } else {
          this.rows = [...rows]
          value = this.rows.map(row => row[this.customReturnField]).join(',')
          this.departNames = this.rows.map(row => row.orgName).join(',')
          this.departIds = value
        }
        this.$emit('change', value)

        this.$emit("setName", this.departNames)
      },
      handleEmpty() {
        this.handleOK()
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    }
  }
</script>

<style lang="less" scoped>
  .components-input-demo-presuffix .anticon-close-circle {
    cursor: pointer;
    color: #ccc;
    transition: color 0.3s;
    font-size: 12px;
  }

  .components-input-demo-presuffix .anticon-close-circle:hover {
    color: #f5222d;
  }

  .components-input-demo-presuffix .anticon-close-circle:active {
    color: #666;
  }
</style>
